﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logistics.DAL
{
    public class TruckDAL
    {
        public static List<Model.Truck> GetTrucks()
        {
            string sql = "select top 3 * from Truck where IsDelete=0";
            SqlDataReader reader = SqlHelper.ExecuteReader(sql, null);
            return TruckList(reader);
        }
        public static List<Model.Truck> TruckList(SqlDataReader reader)
        {
            List<Model.Truck> lst = new List<Model.Truck>();
            while (reader.Read())
            {
                Model.Truck t = new Model.Truck();
                t.TruckID = Convert.ToInt32(reader["TruckID"]);
                t.Number = reader["Number"].ToString();
                t.BuyDate = reader["BuyDate"].ToString();
                t.Type = reader["Type"].ToString();
                t.Length = reader["Length"].ToString();
                t.Tonnage = Convert.ToInt32(reader["Tonnage"]);
                t.FK_TeamID = Convert.ToInt32(reader["FK_TeamID"]);
                t.State = Convert.ToInt32(reader["State"]);
                t.CheckInTime = reader["CheckInTime"].ToString();
                t.AlterTime = reader["AlterTime"].ToString();
                lst.Add(t);
            }
            reader.Close();
            return lst;
        }
        public static int Del(string TruckID)
        {
            string sql = "update Truck set IsDelete=1 where TruckID=@TruckID";
            SqlParameter[] pms = new SqlParameter[]
            {
                new SqlParameter("@TruckID",TruckID)
            };
            return SqlHelper.ExecuteNonQuery(sql, pms);
        }
        public static Model.Truck Eshow(string TruckID)
        {
            string sql = "select * from Truck where TruckID=@TruckID";
            SqlParameter[] pms = new SqlParameter[] {
                new SqlParameter("@TruckID",TruckID)
            };
            SqlDataReader reader = SqlHelper.ExecuteReader(sql, pms);
            return TruckList(reader)[0];
        }
        public static int updatetruck(string FK_TeamID,string State ,string TruckID)
        {
            string sql = "update Truck set FK_TeamID=@FK_TeamID,State=@State where TruckID=@TruckID";
            SqlParameter[] pms = new SqlParameter[]
            {
                new SqlParameter("@TruckID",TruckID),
                new SqlParameter("@FK_TeamID",FK_TeamID),
                new SqlParameter("@State",State)
            };
            return SqlHelper.ExecuteNonQuery(sql, pms);
        }
        public static List<Model.Truck> Getqwe(string Number,string Type, int PageIndex, int PageSize,out int TotalCount)
        {
            SqlParameter[] pms = new SqlParameter[5];
            pms[0] = new SqlParameter("@Number", Number);
            pms[1] = new SqlParameter("@Type", Type);
            pms[2] = new SqlParameter("@PageIndex", PageIndex);
            pms[3] = new SqlParameter("@PageSize", PageSize);
            pms[4] = new SqlParameter("@TotalCount", System.Data.SqlDbType.Int);
            pms[4].Direction = System.Data.ParameterDirection.Output;
            SqlDataReader reader = SqlHelper.ExecuteReader("Proc_GetPagedTruckList1", pms, System.Data.CommandType.StoredProcedure);
            List<Model.Truck> lst = TruckList(reader);
            reader.Close();
            TotalCount = Convert.ToInt32(pms[4].Value);
            return lst;
        }
    }
}
